﻿#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System;
using System.Data;
using UnityEngine;
using UnityEngine.EventSystems;

namespace Neatly.UI
{
    [ExecuteInEditMode]
    [DisallowMultipleComponent]
    public class NEnterButton : NButtonBase, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IBeginDragHandler, IEndDragHandler, IDragHandler
    {
        private Action m_OnPointerEnter;
        private Action m_OnPointerExit;
        private Action m_OnPointerDown;
        private Action m_OnPointerUp;
        private Action m_OnPointerClick;

        private Action<Vector2> m_OnBeginDrag;
        private Action<Vector2> m_OnEndDrag;
        private Action<Vector2> m_OnDrag;

        public int PointerId;
        
        private bool isEnter = false;

        public void OnPointerEnter(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;
            m_CurrentSelectionState = SelectionState.Pressed;
            if (m_ScaleTween & !isEnter)
            {
                UITween.Scale(gameObject, m_SrcScale*NeatlyUI.ButtonScaleSize, NeatlyUI.ButtonTweenTime);
            }
            OnSetProperty();
            
            PointerId = eventData.pointerId;
            m_OnPointerEnter?.Invoke();

            isEnter = true;
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;
            m_CurrentSelectionState = SelectionState.Normal;
            if (m_ScaleTween)
            {
                UITween.Scale(gameObject, m_SrcScale, NeatlyUI.ButtonTweenTime);
            }
            OnSetProperty();
            
            PointerId = eventData.pointerId;
            m_OnPointerExit?.Invoke();
            
            isEnter = false;
        }
        
        public void OnPointerDown(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;

            PointerId = eventData.pointerId;
            m_OnPointerDown?.Invoke();
        }
        
        public void OnPointerClick(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;

            PointerId = eventData.pointerId;
            m_OnPointerClick?.Invoke();
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;
            
            PointerId = eventData.pointerId;
            m_OnPointerUp?.Invoke();
        }
        
        public void OnBeginDrag(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;

            PointerId = eventData.pointerId;
            m_OnBeginDrag?.Invoke(eventData.position);
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;

            PointerId = eventData.pointerId;
            m_OnEndDrag?.Invoke(eventData.position);
        }
        
        public void OnDrag(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;

            PointerId = eventData.pointerId;
            m_OnDrag?.Invoke(eventData.position);
        }

        public void AddPointerEnterListener(Action action)
        {
            m_OnPointerEnter = action;
        }

        public void AddPointerExitListener(Action action)
        {
            m_OnPointerExit = action;
        }

        public void AddPointerDownListener(Action action)
        {
            m_OnPointerDown = action;
        }

        public void AddPointerUpListener(Action action)
        {
            m_OnPointerUp = action;
        }

        public void AddPointerClickListener(Action action)
        {
            m_OnPointerClick = action;
        }

        public void AddBeginDragListener(Action<Vector2> action)
        {
            m_OnBeginDrag = action;
        }
        
        public void AddDragListener(Action<Vector2> action)
        {
            m_OnDrag = action;
        }

        public void AddEndDragListener(Action<Vector2> action)
        {
            m_OnEndDrag = action;
        }

#if UNITY_EDITOR|| UNITY_STANDALONE_WIN
        void Update()
        {
            if (Input.GetKeyUp(virtualKeyCode))
            {
                m_OnPointerExit?.Invoke();
                m_OnPointerDown?.Invoke();
            }
            if (Input.GetKeyDown(virtualKeyCode))
            {
                m_OnPointerEnter?.Invoke();
                m_OnPointerUp?.Invoke();
                m_OnPointerClick?.Invoke();
            }
        }
#endif

    }
}

